package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.resource.date.DateUtils;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;


@Service
@Transactional
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {


    @Autowired
    private OrderSettingMapper orderSettingMapper; //  t_ordersetting 表

    @Autowired
    private  MemberMapper memberMapper;  // t_member

    @Override
    public Order add(Map map) {
        try{
            //   查询  t_ordersetting  根据日期判断当前是否已经预约满
            String  orderDate = (String) map.get("orderDate");
            String  setmeal_Id = (String)map.get("setmealId");
            int setmealId = Integer.parseInt(setmeal_Id);
            String  telephone = (String)map.get("telephone");
            String  name = (String)map.get("name");
            String  sex = (String)map.get("sex");
            String  idCard = (String)map.get("idCard");
            //  select  count(1)  where  orderdate  = ?  and  number>resercation
           int count  =  orderSettingMapper.isOrderOk(orderDate);
           if(count>0){
               //  1.可以预约   :  t_order  生成一张订单
               Order  order = new Order();
               Date date = DateUtils.parseString2Date(orderDate);
               order.setOrderDate(date);
               order.setOrderType(Order.ORDERTYPE_WEIXIN);
               order.setSetmealId(setmealId);
               order.setOrderStatus(Order.ORDERSTATUS_NO);
               //   2.   预约表  已经预约数 +1         update  xx  set  reservations = reservations+1 where orderDate = ?
               orderSettingMapper.updateReservationsByOrderDate(orderDate);
               //  3.   根据手机号 查询会员 是否存在 ， 如果不存在  insert 系统
               Member member = memberMapper.findMemeberBytelephone(telephone);
               if(member==null){
                   //  新会员  insert
                    member = new Member();
                    member.setPhoneNumber(telephone);
                    member.setRegTime(new Date());
                    member.setName(name);
                    member.setIdCard(idCard);
                    member.setSex(sex);
                    memberMapper.insert(member);
               }else{
                   //  4. 老会员    getId()     老会员有可能该套餐 当天已经预约过了！
                    int orderCount =  baseMapper.findOrderIsExistByOrderDateAndSetmealIdAndMemberId(orderDate,setmealId,member.getId());
                    if(orderCount>0){
                         //  老会员 已经预约过了
                        throw  new  RuntimeException("请勿重复预约！");
                    }
               }
               order.setMemberId(member.getId());
               baseMapper.insert(order);//  添加订单
               return  order;
           }else{
               //  预约满了
                 throw  new  RuntimeException("当天预约满了，请改期");
           }
        }
        catch (Exception e){
            e.printStackTrace();
            throw new  RuntimeException(e.getMessage());
        }
    }

    @Override
    public Map findOrderInfoByOrderId(Integer orderid) {

        return baseMapper.findOrderInfoByOrderId(orderid);
    }
}
